package com.spark.gaea.quartz.utils;

import com.google.common.base.Strings;
import org.quartz.CronExpression;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeTask {
	private boolean status;
	private boolean listenerStatus;
	private String type;
	private TaskTimeType taskTimeType;
	private int week;
	private int day;
	private int hour;
	private int minute;
	private int second;
	private int repeatNumber;
	private String cronStr;

	public TimeTask() {
		this.status = false;
		this.listenerStatus = false;
		this.type = null;
		this.taskTimeType = null;
		this.week = 0;
		this.day = 0;
		this.hour = 0;
		this.minute = 0;
		this.second = 0;
		this.repeatNumber = 0;
		this.cronStr = null;
	}

	public boolean setDayPlan(final int hour, final int minute, final int second) {
		this.taskTimeType = TaskTimeType.DayPlan;
		if (hour >= 0 && hour <= 23 && minute >= 0 && minute <= 59 && second >= 0 && second <= 59) {
			this.hour = hour;
			this.minute = minute;
			this.second = second;
			this.type = "01";
			this.status = true;
		} else {
			this.status = false;
		}
		return this.status;
	}

	public boolean setWeekPlan(final int week, final int hour, final int minute, final int second) {
		this.taskTimeType = TaskTimeType.WeekPlan;
		if (week >= 1 && week <= 7 && hour >= 0 && hour <= 23 && minute >= 0 && minute <= 59 && second >= 0
				&& second <= 59) {
			this.week = week;
			this.hour = hour;
			this.minute = minute;
			this.second = second;
			this.type = "01";
			this.status = true;
		} else {
			this.status = false;
		}
		return this.status;
	}

	public boolean setMonthlyPlan(final int day, final int hour, final int minute, final int second) {
		this.taskTimeType = TaskTimeType.MonthlyPlan;
		if (day >= 1 && this.week <= 31 && hour >= 0 && hour <= 23 && minute >= 0 && minute <= 59 && second >= 0
				&& second <= 59) {
			this.day = day;
			this.hour = hour;
			this.minute = minute;
			this.second = second;
			this.type = "01";
			this.status = true;
		} else {
			this.status = false;
		}
		return this.status;
	}

	public boolean setFrequency(final int repeatNumber, final int second) {
		this.taskTimeType = TaskTimeType.Frequency;
		if (second > 0) {
			this.repeatNumber = repeatNumber;
			this.second = second;
			this.type = "02";
			this.status = true;
		} else {
			this.status = false;
		}
		return this.status;
	}

	public boolean setCronExpression(final String cronStr) {
		this.taskTimeType = TaskTimeType.CronExpression;
		if (!Strings.isNullOrEmpty(cronStr)) {
			if (this.cronValidation(cronStr)) {
				this.cronStr = cronStr;
				this.type = "03";
				this.status = true;
			} else {
				this.status = false;
			}
		} else {
			this.status = false;
		}
		return this.status;
	}

	private boolean cronValidation(final String cronStr) {
		try {
			final CronExpression exp = new CronExpression(cronStr);
			final SimpleDateFormat df = new SimpleDateFormat("YYYYMMDD HH:mm:ss");
			Date d = new Date();
			for (int i = 0; i < 3; ++i) {
				d = exp.getNextValidTimeAfter(d);
				System.out.println(df.format(d));
			}
			return true;
		} catch (ParseException e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean isListenerStatus() {
		return this.listenerStatus;
	}

	public void setListenerStatus(final boolean listenerStatus) {
		this.listenerStatus = listenerStatus;
	}

	public boolean isStatus() {
		return this.status;
	}

	public void setStatus(final boolean status) {
		this.status = status;
	}

	public TaskTimeType getTaskTimeType() {
		return this.taskTimeType;
	}

	public void setTaskTimeType(final TaskTimeType taskTimeType) {
		this.taskTimeType = taskTimeType;
	}

	public int getDay() {
		return this.day;
	}

	public void setDay(final int day) {
		this.day = day;
	}

	public int getHour() {
		return this.hour;
	}

	public void setHour(final int hour) {
		this.hour = hour;
	}

	public int getMinute() {
		return this.minute;
	}

	public void setMinute(final int minute) {
		this.minute = minute;
	}

	public int getSecond() {
		return this.second;
	}

	public void setSecond(final int second) {
		this.second = second;
	}

	public int getWeek() {
		return this.week;
	}

	public void setWeek(final int week) {
		this.week = week;
	}

	public int getRepeatNumber() {
		return this.repeatNumber;
	}

	public void setRepeatNumber(final int repeatNumber) {
		this.repeatNumber = repeatNumber;
	}

	public String getCronStr() {
		return this.cronStr;
	}

	public void setCronStr(final String cronStr) {
		this.cronStr = cronStr;
	}

	public String getType() {
		return this.type;
	}

	public void setType(final String type) {
		this.type = type;
	}

	public enum TaskTimeType {
		DayPlan, WeekPlan, MonthlyPlan, Frequency, CronExpression
	}
}
